# -*- coding: utf-8 -*-

from rest_framework.decorators import action
from core.viewset import CustomModelViewSet
from core.response import JsonResponse
from apps.pms.models import Version
from utils.tree import list2tree
from apps.pms.version.serializers import *
from apps.pms.version.resources import *
from apps.pms.version.filters import *


class VersionViewSet(CustomModelViewSet):
    """
    版本管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """

    queryset = Version.objects.all()
    serializer_class = VersionSerializer
    list_serializer_class = ListVersionSerializer
    filterset_class = VersionFilterSet
    import_resources = VersionResouces
    export_resources = VersionResouces

    @action(detail=False, methods=["GET"])
    def treeselect(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = VersionTreeSelectSerializer(queryset, many=True, request=request)
        data = list2tree(serializer.data)
        return JsonResponse(data=data, msg="获取成功")
